import { defineStore } from "pinia";
import { ref, computed } from 'vue'
import { useUserStore } from './userStore.js'
import { findNewCartListAPI, insertCarAPI, deleteCartAPI } from '@/apis/cart.js'

export const useCartStore = defineStore("cart", () => {
    const userStore = useUserStore();
    const isLogin = computed(() => userStore.userInfo.token)
    // 1. 定义state - carList
    const cartList = ref([])

    const updateNewList = async () => {
        const res = await findNewCartListAPI();
        cartList.value = res.result
    }

    // 2. 定义action - addCar
    const addCart = async (cart) => {
        if (isLogin.value) {
            // 登录之后加入购物车逻辑
            const {skuId, count} = cart;
            await insertCarAPI({skuId, count}) // 加入购物车
            const res = await findNewCartListAPI(); // 获取最新购物车数据
            cartList.value = res.result // 覆盖旧数据
        } else {
            // 未登录加入购物车逻辑
            // 添加购物车操作
            const finded = cartList.value.find(item => item.skuId === cart.skuId);
            // 添加过的 count + 1
            if (finded) {
                finded.count += cart.count
            } else {
                // 没有添加过的直接 push
                cartList.value.push(cart)
            }
        }
    }
    // 删除购物车
    const delCart = async (skuId) => {
        if (isLogin.value) {
            // 登录之后删除购物车逻辑
            await deleteCartAPI([skuId]);
            const res = await findNewCartListAPI();
            cartList.value = res.result
        } else {
            // 未登录删除购物车逻辑
            // cartList.value.splice(cartList.value.indexOf(skuId), 1) // 找到要删除项的下标值，然后粘接
            cartList.value = cartList.value.filter(item => item.skuId !== skuId) // 过滤实现删除
        }
    }
    // 清除购物车
    const clearCart = async () => {
        cartList.value = []
    }
    // 全选功能
    const allCheck = (select) => {
        cartList.value.forEach(item => item.select = select)
    }

    // 计算属性
    // 1. 总的数量
    const totalCount = computed(() => cartList.value.reduce((accumulator, item) => accumulator += item.count, 0))
    // 2. 总价
    const totalMoney = computed(() => cartList.value.reduce((accumulator, item) => accumulator += item.count * item.price, 0))
    // 3. 是否全选
    const isAll = computed(() => cartList.value.every(item => item.select))
    // 4. 已选择商品价钱合计
    const selectedMoney = computed(() => cartList.value.filter(item => item.select).reduce((accumulator, item) => accumulator += item.count * item.price, 0))
    // 5. 已选择商品数量
    const selectedCount = computed(() => cartList.value.filter(item => item.select).reduce((accumulator, item) => accumulator += item.count, 0))
    // 单选功能
    const singCheck = (select, skuId) => {
        // 通过skuId 找到对应的商品
        const item = cartList.value.find(item => item.skuId === skuId);
        item.select = select
    }

    return {
        cartList,
        addCart,
        delCart,
        totalCount,
        totalMoney,
        singCheck,
        isAll,
        allCheck,
        selectedCount,
        selectedMoney,
        clearCart,
        updateNewList
    }

}, {
    persist: true
})